package thimeeeee.Model;

//import thimeeeee.Skeleton;

public class FallingElement extends MovingElement {
	protected boolean _isFalling;
    private double timeStamp;
    
    public FallingElement(){
//        Skeleton.Create("FallingElement");
    }

	@Override
    public void Idle(double aTimeStamp)
    {
//        Skeleton.StartFunc("FallingElement.Idle()");
        if (aTimeStamp > timeStamp)
        {
            Field MyUnderField = MyField.GetNeighbor(Direction.down);

            if(MyUnderField != null)
            {
                if(MyUnderField.IsEmpty())
                {
                    //esni kezd
                    this._isFalling = true;
                    MoveTo(MyUnderField);
                }
                else
                {
                    
                    ElementBase[] neighborElements = MyUnderField.GetElements();
                    boolean canIGo = true;

                    for(int i = 0; i < neighborElements.length; i++)
                    {
                        if(neighborElements[i] != null) canIGo = (canIGo && (neighborElements[i].MeetFallingElement(this)));
                    }

                    this._isFalling = false;
                    
                    if(canIGo)
                    {
                        //esni kezd
                        this._isFalling = true;
                        MoveTo(MyUnderField);
                    }
                }
            }
        }
        timeStamp = aTimeStamp;
//        Skeleton.EndFunc("FallingElement.Idle()", "void");
    }

    @Override
    public boolean MeetFallingElement(FallingElement fe)
    {
//        Skeleton.StartFunc("FallingElement.MeetFallingElement()");
        fe.Collapse();
//        Skeleton.EndFunc("FallingElement.MeetFallingElement()", "false");
        return false;
    }

    public void Collapse()
    {
//        Skeleton.StartFunc("FallingElement.Collapse()");

        Field dLeftNeighbor = MyField.GetNeighbor(Direction.downleft);
        Field dRightNeighbor = MyField.GetNeighbor(Direction.downright);

        if (dLeftNeighbor.IsEmpty())
        {
            MoveTo(dLeftNeighbor);
        }
        else if (dRightNeighbor.IsEmpty())
        {
            MoveTo(dRightNeighbor);
        }

//        Skeleton.EndFunc("FallingElement.Collapse()", "void");
    }


}